package com.example.demo.c;

import java.util.Date;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/es")
public class ESController {

	@Autowired
	private Client client; 
	@RequestMapping("/testes")
	public String testes(){
		EUser user = new EUser();
		user.setUsername("测试");
		user.setBirth(new Date());
		user.setEmail("fs@11.com");
		IndexRequestBuilder requestBuilder = client.prepareIndex("test", "user", "1");// 设置索引名称，索引类型，id
		IndexResponse indexResponse = requestBuilder.setSource(JsonHelper.toJsonString(user)).execute().actionGet();// 创建索引
		System.out.println(indexResponse.toString());
		// 搜索数据
		GetResponse response = client.prepareGet("test", "user", "1").execute().actionGet();
		// 输出结果
		System.out.println(response.getSourceAsString());
		return response.getSourceAsString();
	}

	

}
